package Api;

import Util.HttpUtil;
import Util.MyKey;
import Util.SignEncrypt;
import com.alibaba.fastjson.JSONObject;
import org.bouncycastle.util.encoders.Base64;

import java.net.URLEncoder;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static Util.Tools.MaptoStr;
import static Util.Tools.getMD5;


/**
 * 卡bin查询 v2
 *
 */


public class BankCardBinQueryAPIV2 {
    public static void main(String[] args) {
        try {
            //目标URL
            String url = "https://pay.heepay.com/Payment/BankCardBinQueryAPIV2.aspx";

            //需要的MD5密钥
            String key = MyKey.MD5Key1;
            //商户编号，（汇付宝商户内码：七位整数数字）
            String agent_id = "1664502";
            //固定版本号
            String version = "2";

            // 获取当前14位时间戳
            Date now = new Date();
            // 创建 SimpleDateFormat 对象，指定日期格式
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            String timestamp = dateFormat.format(now);
            //固定编码
            String charset = "utf-8";
            //固定签名方式
            String sign_type = "MD5";


            /**
             * 需要查询的银行卡号！！！
             */

            String bank_card_no = "6235753600002797019";

            //初始化RSA密钥
            String publicKeystr = MyKey.publicKeystr1;
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            byte[] publicKeyPath = Base64.decode(publicKeystr.getBytes());
            PublicKey heepayPublicKey = keyFactory.generatePublic(new X509EncodedKeySpec(publicKeyPath));
            SignEncrypt signEncrypt = new SignEncrypt();

            //加密银行卡号
            String biz_bank_card_no = signEncrypt.encrypt(bank_card_no, heepayPublicKey);

            //生成biz串
            JSONObject biz_content = new JSONObject();
            biz_content.put("bank_card_no", biz_bank_card_no);

            //签名串组装
            String OldSign = "agent_id=" + agent_id
                    +"&biz_content=" + biz_content
                    +"&charset=" + charset
                    +"&sign_type=" + sign_type
                    +"&timestamp=" + timestamp
                    +"&version=" + version
                    +"&key=" + key;

            System.out.println("签名串：--------------------------->"+OldSign);
            System.out.println();


            //MD5加密得出签名结果
            String sign = getMD5(OldSign);

            //最终请求的biz_content的值需要urlencode
            String biz_content1 = URLEncoder.encode(String.valueOf(biz_content), "UTF-8");

            // 请求参数，以Map形式表示
            Map<String, String> parameters = new HashMap<>();
            parameters.put("agent_id", agent_id);
            parameters.put("charset", charset);
            parameters.put("timestamp", timestamp);
            parameters.put("sign_type", sign_type);
            parameters.put("version", version);
            parameters.put("biz_content", biz_content1);
            assert sign != null;
            parameters.put("sign", sign.toUpperCase());

            //打印请求参数
            String Str = MaptoStr(parameters);
            System.out.println("发送请求数据：--------------------------->"+ Str);
            System.out.println();

            String Result = HttpUtil.sendPost2(url, parameters);
            System.out.println("请求响应结果：--------------------------->"+Result);
        } catch (
                Exception e) {
            throw new RuntimeException(e);
        }
    }
}

